package com.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.common.constant.SharesConstan;
import com.common.utils.DateUtil;
import com.common.utils.HttpClient5Utils;
import com.common.utils.StringUtil;
import com.entity.SharesDetail;
import com.entity.SharesGdnum;
import com.mapper.SharesDetailMapper;
import com.service.SharesDetailService;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author pumpkin
 * @since 2021-10-25
 */
@Service
public class SharesDetailServiceImpl extends ServiceImpl<SharesDetailMapper, SharesDetail> implements SharesDetailService {

    @Override
    public void updateCprice(List<SharesGdnum> gdList) {
        System.out.println("公布时候的价格 start");
        if(CollectionUtils.isEmpty(gdList))    {
                 return;
         }

        //查询最新统计的日期
        Wrapper<SharesDetail> wr=new EntityWrapper<>();
        wr.eq("status", SharesConstan.ALL_SHARES);
        List<SharesDetail> sharesDetails = this.selectList(wr);

        Map<String,SharesGdnum> gdnumMap=new HashMap<>();
            for (SharesGdnum gdnum : gdList) {
            String code = gdnum.getCode();
            gdnumMap.put(code,gdnum);
        }

        for (SharesDetail sharesDetail : sharesDetails) {
            String code = sharesDetail.getCode();
            String codeSub = code.substring(2);
            SharesGdnum sharesGdnum = gdnumMap.get(codeSub);
            if(null==sharesGdnum){
                continue;
            }

            //查询当天收盘价
            String endDate = sharesGdnum.getEndDate().substring(0,10);

            String  url="http://49.push2his.eastmoney.com/api/qt/stock/kline/get" +
                    "?cb=jQuery112408735255525730368_" + +new Date().getTime()+
                    "&secid=0." +codeSub +
                    "&ut=fa5fd1943c7b386f172d6893dbfba10b" +
                    "&fields1=f1%2Cf2%2Cf3%2Cf4%2Cf5%2Cf6" +
                    "&fields2=f51%2Cf52%2Cf53%2Cf54%2Cf55%2Cf56%2Cf57%2Cf58%2Cf59%2Cf60%2Cf61" +
                    "&klt=101&fqt=1&end=20500101&lmt=120&_="+new Date().getTime();


            try{
                HttpGet get=new HttpGet(url);
                CloseableHttpResponse execute = HttpClient5Utils.httpclient.execute(get);
                String result = EntityUtils.toString(execute.getEntity(), "UTF-8");
                int rIndex = result.indexOf("(");
                String resultStr = result.substring(rIndex + 1, result.length() - 2);
                JSONObject datas2 = JSONObject.parseObject(resultStr);
                String data = datas2.getString("data");
                String klines = JSONObject.parseObject(data).getString("klines");
                List<Object> parse = (List<Object>) JSON.parse(klines);

                //收盘价时间
                String cPrice = "";
                Map<String,String>  pMap=new HashMap<>();
                if (!CollectionUtils.isEmpty(parse)){
                    for (Object o : parse) {
                        String[] splito = o.toString().split(",");
                        BigDecimal spj = new BigDecimal(splito[2]);//收盘价
                        pMap.put(splito[0],spj.toString());
                    }
                }
                cPrice=pMap.get(endDate);
                for (int i=1;i<10;i++){
                    if(StringUtil.isEmpty(cPrice)){
                        String ymdByDaysAgo = DateUtil.getYMDByDaysAgo(endDate, i);
                        cPrice=pMap.get(ymdByDaysAgo);
                    }
                }

                if(StringUtil.isEmpty(cPrice)){
                    System.out.println("err 记录季报收盘价"+codeSub+",time="+endDate);
                    continue;
                }
                //记录当天的收盘价
                sharesDetail.setZf5(cPrice);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        this.updateBatchById(sharesDetails);
        System.out.println("公布时候的价格 end");
    }

}
